package com.kristin.io.file;

import java.io.*;

public class FileCopy {

    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        copy();
        long end = System.currentTimeMillis();
        System.out.println("复制完成时间为:" + (end - start) + " ms");
    }

    public static void copy() throws IOException {
        File sourceFile = new File("D:\\a.mp4");
        File targetFile = new File("D:\\a_copy.mp4");

        FileInputStream source = new FileInputStream(sourceFile);
        FileOutputStream target = new FileOutputStream(targetFile);

        // 增加字节缓冲流
        BufferedInputStream bis = new BufferedInputStream(source);
        BufferedOutputStream bos = new BufferedOutputStream(target);

        int i;
        while((i = source.read()) != -1) {
            target.write(i);
            target.flush();
        }
        source.close();
        target.close();
    }

    public static void copyByBuffer() throws IOException {
        File sourceFile = new File("D:\\a.mp4");
        File targetFile = new File("D:\\a_copy_buffer.mp4");

        FileInputStream source = new FileInputStream(sourceFile);
        FileOutputStream target = new FileOutputStream(targetFile);

        // 增加字节缓冲流
        BufferedInputStream bis = new BufferedInputStream(source);
        BufferedOutputStream bos = new BufferedOutputStream(target);

        int i;
        while((i = bis.read()) != -1) {
            bos.write(i);
        }
        bos.flush();
        bis.close();
        bos.close();
    }


}
